/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['super_admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}
/**
 * 传真
 * @param {string} fax
 */
 export function faxFormat(fax) {
  const reg = /^(?:\d{3,4}-)?\d{7,8}(?:-\d{1,6})?$/
  return reg.test(fax)
}
/**
 * @param {string} cellphone
 * @returns {Boolean}
 */
export function validateCellphone(phone) {
  const reg = /^[1][0-9]{10}$/
  return reg.test(phone)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
/**
 * 座机号码
 * @param {string} telePhone
 */
export function validateTelephone(telePhone) {
  const reg = /^([0-9-]+)?\d{5}$/
  return reg.test(telePhone)
}
/**
 * 手机号
 * @param {string} phone
 */
export function validatePhone(phone) {
  const reg = /^[1][0-9]{10}$/
  return reg.test(phone)
}
/**
 * 正数
 * @param {string} number
 */
export function validatenumber(number) {
  const reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
  return reg.test(number)
}
/**
 * 数字
 * @param {string} number
 */
export function isNumber(number) {
  const reg = /^(([^0][0-9]+|0)\.([0-9]+)$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]+)$)|^(([1-9]+)$)/
  return reg.test(number)
}

/**
 * 身份证
 * @param {string} IDNumber
 */
export function validateIDNumber(IDNumber) {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return reg.test(IDNumber)
}

/**
 * 车牌号
 * @param {string} carNum
 */
export function validateCarNum(carNum) {
  const reg = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/
  return reg.test(carNum)
}

/**
 * 银行卡号
 * @param {string} bankCardNum
 */
export function validateBankCardNum(bankCardNum) {
  const reg = /^([1-9]{1})(\d{14}|\d{15}|\d{17}|\d{18})$/
  return reg.test(bankCardNum)
}

/**
 * 统一社会信用代码
 * @param {string} businessLicenseImgNum
 */
export function validateBusinessLicenseImgNum(businessLicenseImgNum) {
  const reg = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}/g
  return reg.test(businessLicenseImgNum)
}

/**
 * 简称码
 * @param {string} prefix
 */
export function validatePrefix(prefix) {
  const reg = /^[a-zA-Z]{2,4}$/
  return reg.test(prefix) || !prefix
}

/**
 * 登录密码
 * @param {string} pwd
 */
export function validatePassword(pwd) {
  const reg = /^(?=.*?[0-9])(?=.*?[a-z])[0-9a-z]{8,}$/
  return reg.test(pwd)
}
// 验证传真
export function validateFax(fax) {
  const reg = /^(\d{3,4}-)?\d{7,8}$/
  return reg.test(fax)
}
// 姓名验证
export function validateName(name) {
  const reg = /^[a-zA-Z\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/
  return reg.test(name)
}
// 正整数
export function validateInteger(number) {
  const reg = /^[0-9]*[1-9][0-9]*$/
  return reg.test(number)
}
// 网址
export function validateUrl(url) {
  const reg = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
  return reg.test(url)
}
// 判断字符是否为空
export function isEmpty(str) {
  if (str === '') return true
  const reg = /^[ ]+$"/
  return reg.test(str)
}
/**
 * [isDefine 判断值是否定义]
 * @param  {[string]}  value
 * @return {Boolean}
 */
 export function isDefine(value) {
  if (
    value === null ||
    value === '' ||
    value === 'undefined' ||
    value === undefined ||
    value === 'null' ||
    value === '(null)' ||
    value === 'NULL' ||
    typeof value === 'undefined'
  ) {
    return false
  } else {
    value = value + ''
    value = value.replace(/\s/g, '') //全局匹配空白字符
    if (value === '') {
      return false
    }
    return true
  }
}
